package com.dayuanit.atm.task;

import com.dayuanit.atm.entity.Transfer;
import com.dayuanit.atm.mapper.TransferMapper;
import com.dayuanit.atm.service.BankCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 处理待返回的转账订单
 */
@Component("transferCancelTask")
public class TransferCancelTask {

    @Autowired
    private TransferMapper transferMapper;

    @Autowired
    private BankCardService bankCardService;

    public void doTask() {
        System.out.println("transferCancelTask doTask()");
        processCancel();
    }

    private void processCancel() {
        // 分页查询待处理的转账订单
        int currentPage = 1;
        int prePageNum = 2;
        int offset = 0;

        do {
            offset = (currentPage - 1) * prePageNum;
            final List<Transfer> transfers = transferMapper.listTransfer(null, 2, offset, 2);

            for (Transfer transfer : transfers) {
                // 处理每一条转账待返回的记录
                System.out.println(">>>>转账记录ID=" + transfer.getId());
                try {
                    bankCardService.transferCancel(transfer);
                } catch (Exception e) {
                    e.printStackTrace();
                    //TODO 如果处理超过多次后 转入人工处理流程
                }
            }

            currentPage ++;
            if (transfers.size() <= 0) {
                break;
            }
        } while(true);
    }
}
